import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:openim_common/openim_common.dart';

class TitleBar extends StatelessWidget implements PreferredSizeWidget {
  const TitleBar({
    Key? key,
    this.height,
    this.left,
    this.center,
    this.right,
    this.backgroundColor,
    this.showUnderline = false,
  }) : super(key: key);
  final double? height;
  final Widget? left;
  final Widget? center;
  final Widget? right;
  final Color? backgroundColor;
  final bool showUnderline;

  @override
  Widget build(BuildContext context) {
    final mq = MediaQuery.of(context);
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark,
      child: Container(
        color: backgroundColor ?? Styles.c_FFFFFF,
        padding: EdgeInsets.only(top: mq.padding.top),
        child: Container(
          height: height,
          padding: EdgeInsets.symmetric(horizontal: 16.w),
          decoration: showUnderline
              ? BoxDecoration(
                  border: BorderDirectional(
                    bottom: BorderSide(color: Styles.c_E8EAEF, width: .5),
                  ),
                )
              : null,
          child: Row(
            children: [
              if (null != left) left!,
              if (null != center) center!,
              if (null != right) right!,
            ],
          ),
        ),
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(height ?? 44.h);

  TitleBar.conversation(
      {super.key,
      String? statusStr,
      bool isFailed = false,
      Function()? onAddFriend,
      Function()? onAddGroup,
      Function()? onCreateGroup,
      Function()? onScan, // 新增扫一扫回调
      Function()? onSearch, // 新增搜索回调
      CustomPopupMenuController? popCtrl,
      this.left})
      : backgroundColor = null,
        height = 62.h,
        showUnderline = false,
        center = null,
        right = Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 搜索按钮
            if (onSearch != null)
              GestureDetector(
                onTap: onSearch,
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 8.w),
                  child: ImageRes.searchBlack.toImage
                    ..width = 28.w
                    ..height = 28.h,
                ),
              ),
            // 加号按钮
            PopButton(
              popCtrl: popCtrl,
              menus: [
                PopMenuInfo(
                  text: StrRes.addFriend,
                  icon: ImageRes.popMenuAddFriend,
                  onTap: onAddFriend,
                ),
                // PopMenuInfo(
                //   text: StrRes.addGroup,
                //   icon: ImageRes.popMenuAddGroup,
                //   onTap: onAddGroup,
                // ),
                PopMenuInfo(
                  text: StrRes.createGroup,
                  icon: ImageRes.popMenuCreateGroup,
                  onTap: onCreateGroup,
                ),
                PopMenuInfo(
                  text: StrRes.scan,
                  icon: ImageRes.popMenuScan,
                  onTap: onScan,
                ),
              ],
              child: ImageRes.addBlack.toImage
                ..width = 28.w
                ..height = 28.h /*..onTap = onClickAddBtn*/,
            ),
          ],
        );

  TitleBar.chat({
    super.key,
    String? title,
    String? member,
    bool isMultiModel = false,
    bool showCallBtn = true,
    bool isMuted = false,
    Function()? onClickCallBtn,
    Function()? onClickMoreBtn,
    Function()? onCloseMultiModel,
    List<Widget>? actions, // 新增 actions 参数
  })  : backgroundColor = null,
        height = 48.h,
        showUnderline = true,
        center = Flexible(
            child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            if (null != title)
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Flexible(
                      flex: 5,
                      child: Container(
                        child: title.trim().toText
                          ..style = Styles.ts_0C1C33_17sp_semibold
                          ..maxLines = 1
                          ..overflow = TextOverflow.ellipsis
                          ..textAlign = TextAlign.center,
                      )),
                    // 根据设置显示或隐藏群人数
                    if (member != null && member.isNotEmpty)
                      member.toText
                        ..style = Styles.ts_0C1C33_17sp_semibold
                        ..maxLines = 1
                        ..overflow = TextOverflow.ellipsis,
                ],
              ),
          ],
        )),
        left = SizedBox(
            width: showCallBtn ? 48.w : 48.w,
            child: isMultiModel
                ? (StrRes.cancel.toText
                  ..style = Styles.ts_0C1C33_17sp
                  ..onTap = onCloseMultiModel)
                : (ImageRes.backBlack.toImage
                  ..width = 24.w
                  ..height = 24.h
                  ..onTap = (() => Get.back()))),
        right = SizedBox(
            width: actions != null 
                ? (actions.length * 48.w) + 16.w  // 根据 actions 数量动态计算宽度
                : 16.w + (showCallBtn ? 56.w : 28.w),
            child: actions != null 
                ? Row(
                    mainAxisSize: MainAxisSize.min,
                    children: actions,
                  )
                : Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      if (showCallBtn)
                        ImageRes.callBack.toImage
                          ..width = 28.w
                          ..height = 28.h
                          ..opacity = isMuted ? 0.4 : 1
                          ..onTap = isMuted ? null : onClickCallBtn,
                      16.horizontalSpace,
                      ImageRes.moreBlack.toImage
                        ..width = 28.w
                        ..height = 28.h
                        ..onTap = onClickMoreBtn,
                    ],
                  ));

  
  TitleBar.back({
    super.key,
    String? title,
    String? leftTitle,
    TextStyle? titleStyle,
    TextStyle? leftTitleStyle,
    String? result,
    Color? backgroundColor,
    Color? backIconColor,
    Widget? right, // 改为局部参数，不使用 this.right
    this.showUnderline = false,
    bool centerTitle = false, // 新增居中参数
    Function()? onTap,
  })  : height = 44.h,
        backgroundColor = backgroundColor ?? Styles.c_FFFFFF,
        center = centerTitle 
            ? Expanded(
                child: (title ?? '').toText
                  ..style = (titleStyle ?? Styles.ts_0C1C33_17sp_semibold)
                  ..textAlign = TextAlign.center)
            : Expanded(
                child: (title ?? '').toText
                  ..style = (titleStyle ?? Styles.ts_0C1C33_17sp_semibold)
                  ..textAlign = TextAlign.center),
        left = centerTitle 
            ? SizedBox(
                width: 56.w, // 固定左侧宽度以确保居中
                child: GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: onTap ?? (() => Get.back(result: result)),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      ImageRes.backBlack.toImage
                        ..width = 24.w
                        ..height = 24.h
                        ..color = backIconColor,
                      if (null != leftTitle) leftTitle.toText..style = (leftTitleStyle ?? Styles.ts_0C1C33_17sp_semibold),
                    ],
                  ),
                ),
              )
            : GestureDetector(
                behavior: HitTestBehavior.translucent,
                onTap: onTap ?? (() => Get.back(result: result)),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    ImageRes.backBlack.toImage
                      ..width = 24.w
                      ..height = 24.h
                      ..color = backIconColor,
                    if (null != leftTitle) leftTitle.toText..style = (leftTitleStyle ?? Styles.ts_0C1C33_17sp_semibold),
                  ],
                ),
              ),
        right = centerTitle 
            ? SizedBox(
                width: 56.w, // 固定右侧宽度以确保居中
                child: right ?? Container(),
              )
            : right;

  TitleBar.contacts({
    super.key,
    this.showUnderline = false,
    Function()? onClickAddContacts,
  })  : height = 44.h,
        backgroundColor = Styles.c_FFFFFF,
        center = Spacer(),
        left = StrRes.contacts.toText..style = Styles.ts_0C1C33_20sp_semibold,
        right = Row(
          children: [
            16.horizontalSpace,
            ImageRes.addContacts.toImage
              ..width = 28.w
              ..height = 28.h
              ..onTap = onClickAddContacts,
          ],
        );

  TitleBar.workbench({
    super.key,
    this.showUnderline = false,
  })  : height = 44.h,
        backgroundColor = Styles.c_FFFFFF,
        center = null,
        left = StrRes.workbench.toText..style = Styles.ts_0C1C33_20sp_semibold,
        right = null;

  TitleBar.search({
    super.key,
    String? hintText,
    TextEditingController? controller,
    FocusNode? focusNode,
    bool autofocus = true,
    Function(String)? onSubmitted,
    Function()? onCleared,
    ValueChanged<String>? onChanged,
  })  : height = 44.h,
        backgroundColor = Styles.c_FFFFFF,
        center = Expanded(
          child: Container(
              child: SearchBox(
            enabled: true,
            autofocus: autofocus,
            hintText: hintText,
            controller: controller,
            focusNode: focusNode,
            onSubmitted: onSubmitted,
            onCleared: onCleared,
            onChanged: onChanged,
          )),
        ),
        showUnderline = true,
        right = null,
        left = ImageRes.backBlack.toImage
          ..width = 24.w
          ..height = 24.h
          ..onTap = (() => Get.back());
}
